# read in Cropland 2000 tiff file and write out as ascii to read in to stata
# data initially downloaded from http://www.earthstat.org/cropland-pasture-area-2000/
# but link no longer seems to work
# looks like can now find tif file at: 
# https://data.earthdata.nasa.gov/nasa-earth/human-dimensions/sedac-root/downloads/data/aglands/aglands-croplands-2000/gl-croplands-geotif.zip


# Start the clock
ptm <- proc.time()


library(raster)



infile <- "../data/Cropland2000_5m.tif"
outfile <- "../data/cropland2000.asc"

r <- raster(infile)

#aggregate to half degrees because can't merge to .05 degree data
ra <- aggregate(r, fact=6, na.rm=TRUE, fun=mean)

#crop at lat=70 to -60 to match droughts
rc <- crop(ra, extent(ra, 31, 300, 1, 720))

writeRaster(rc, filename=outfile, format = "ascii", overwrite=TRUE, NAflag=-9999)

# Stop the clock
proc.time() - ptm